import 'package:flutter/material.dart';
import 'package:flutter_shop/pages/cart/cart_tab_page/entity/cart_goods_data.dart';
import 'package:flutter_shop/pages/cart/cart_tab_page/provider/cart_provider.dart';
import 'package:flutter_shop/pages/cart/cart_tab_page/widget/cart_counter.dart';
import 'package:flutter_shop/res/colors.dart';
import 'package:flutter_shop/res/dimens.dart';
import 'package:flutter_shop/res/styles.dart';
import 'package:flutter_shop/utils/utils.dart';
import 'package:flutter_shop/widget/custom_dialog.dart';
import 'package:flutter_shop/widget/load_image.dart';
import 'package:flutter_shop/widget/water_click.dart';
import 'package:provider/provider.dart';

///购物车商品条目
class ItemCartGoods extends StatelessWidget {
  final CartGoodsData item;

  ItemCartGoods({Key key, this.item}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return Container(
      padding: Paddings.common,
      decoration: BoxDecoration(
        color: CommonColors.white,
        border: Borders.bottom,
      ),
      child: Row(
        children: <Widget>[
          _checkBox(context),
          Gaps.h_gap_10,
          _goodsImage(),
          Gaps.h_gap_10,
          Expanded(child: _goodsNameAndCounter()),
          _priceAndDelete(context),
        ],
      ),
    );
  }

  Widget _checkBox(BuildContext context) {
    return WaterClick(
      child: Icon(
        item.checked ? Icons.check_circle : Icons.radio_button_unchecked,
        color: item.checked ? CommonColors.red : CommonColors.gray,
        size: 25.0,
      ),
      onPressed: () {
        Provider.of<CartProvider>(context, listen: false).checkItem(item.goodsId);
      },
    );
  }

  Widget _goodsImage() {
    return Container(
      decoration: BoxDecoration(border: Border.all(color: CommonColors.common_divide_line)),
      child: LoadImage(
        item.image,
        width: 80,
        height: 80,
        fit: BoxFit.cover,
      ),
    );
  }

  Widget _goodsNameAndCounter() {
    return Container(
      height: 80,
      child: Column(
        mainAxisAlignment: MainAxisAlignment.spaceAround,
        crossAxisAlignment: CrossAxisAlignment.start,
        children: <Widget>[
          Expanded(
            child: Text(
              item.goodsName,
              style: TextStyles.common_text,
              maxLines: 2,
              overflow: TextOverflow.ellipsis,
            ),
          ),
          Gaps.v_gap_10,
          CartCounter(item: item),
        ],
      ),
    );
  }

  Widget _priceAndDelete(BuildContext context) {
    return Container(
      width: 80,
      height: 80,
      child: Column(
        crossAxisAlignment: CrossAxisAlignment.end,
        children: <Widget>[
          Text("¥${item.price}",
              style: TextStyle(
                fontSize: CommonDimens.font_size_middle,
                color: CommonColors.red,
              )),
          Text(
            "¥${item.oriPrice}",
            style: TextStyle(
              fontSize: CommonDimens.font_size_small,
              color: CommonColors.gray,
              decoration: TextDecoration.lineThrough,
            ),
          ),
          Expanded(child: Gaps.empty),
          WaterClick(
            onPressed: () {
              Utils.showCustomDialog(
                context,
                ConfirmDialog("确定删除吗?", confirmCallback: () {
                  Provider.of<CartProvider>(context, listen: false).deleteById(item.goodsId);
                }),
              );
            },
            child: Icon(
              Icons.delete_sweep,
              color: CommonColors.gray,
              size: 30,
            ),
          )
        ],
      ),
    );
  }
}
